﻿using DataViewTool.Models;
using Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication2;
using WindowsFormsApplication2.Models;

namespace DataViewTool
{
    public partial class MyDataFrm : Form
    {
  
        public MyDataFrm()
        {
            InitializeComponent();
        }

        private void DataFrm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            DataComBox.DataSource = Program.DataMoudels.Models.ToList();
            DataComBox.DisplayMember = "Name";
            DataComBox.ValueMember = "Name";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name= txtName.Text.Trim();
            var model = Program.DataMoudels.Models.Find(m => m.Name == name);
            if (model == null) {
                model = new DataModel();
                Program.DataMoudels.Models.Add(model);
            }
            model.Name = name;
            model.Host = txtHost.Text.Trim();
            model.UserName = txtUserName.Text.Trim();
            model.Password = txtPassword.Text.Trim();
            model.DataBase = txtDataBase.Text.Trim();
            model.Port = txtPort.Text.Trim();
            Program.Save();
            BindData();
            DataComBox.SelectedText = name;
            DataComBox.SelectedValue = name;
        }

        private void DataComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Name = DataComBox.Text;
            var model = Program.DataMoudels.Models.Find(m => m.Name == Name);
            if (model == null)
            {
                txtHost.Text="";
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtDataBase.Text = "";
                txtName.Text = "";
                txtPort.Text = "3306";
            }
            else {
                txtHost.Text = model.Host;
                txtUserName.Text =model.UserName;
                txtPassword.Text = model.Password;
                txtDataBase.Text = model.DataBase;
                txtName.Text =model.Name;
                txtPort.Text =model.Port;
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                string Host = txtHost.Text.Trim();
                string UserName = txtUserName.Text.Trim();
                string Password = txtPassword.Text.Trim();
                string DataBase = txtDataBase.Text.Trim();
                string Port = txtPort.Text.Trim();
                string conStr = string.Format("Database={4};Data Source={0};User Id={1};Password={2};pooling=false;CharSet=utf8;port={3}", Host,UserName,Password,Port,DataBase);
                String sql = string.Format("SELECT table_name as tablename,table_comment as tablecoment FROM information_schema.tables WHERE table_schema='{0}' AND table_type='base table';",DataBase);
                MySqlHelper mysql = new MySqlHelper(conStr);
                List<TableInfo> list = mysql.ExecuteObjects<TableInfo>(sql);
                MessageBox.Show("测试成功","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch (Exception err) {
                MessageBox.Show("测试失败"+err.ToString(), "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string Name = DataComBox.Text;
            var model = Program.DataMoudels.Models.Find(m => m.Name == Name);
            if (model != null)
            {
                Program.DataMoudels.Models.Remove(model);
                Program.Save();
                BindData();
            }
        }
    }
}
